//
//  AFUserDetailCell.m
//  AFChat
//
//  Created by Void on 2018/6/22.
//  Copyright © 2018年 afei. All rights reserved.
//

#import "AFMineHeaderCell.h"

#define     MINE_SPACE_X        14.0f
#define     MINE_SPACE_Y        12.0f
@interface AFMineHeaderCell ()

@property (nonatomic, strong) UILabel *wechatIDLabel;
@property (nonatomic, strong) UILabel *nicknameLabel;
@property (nonatomic, strong) UIImageView *avatarImageView;
@property (nonatomic, strong) UIImageView *QRImageView;

@end

@implementation AFMineHeaderCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        [self setBackgroundColor:[UIColor whiteColor]];
        UIView *bgView = [[UIView alloc] initWithFrame: self.bounds];
        bgView.backgroundColor = [UIColor colorGrayLine];
        [self setSelectedBackgroundView:bgView];
        [self.contentView addSubview:self.avatarImageView];
        [self.contentView addSubview:self.nicknameLabel];
        [self.contentView addSubview:self.wechatIDLabel];
        [self.contentView addSubview:self.QRImageView];
        [self p_addMasonry];
    }
    return self;
}

- (void)setUser:(AFUser *)user {
    _user = user;
    if (user.avatarPath) {
        [self.avatarImageView setImage:[UIImage imageNamed:user.avatarPath]];
    } else {
        [self.avatarImageView tt_setImageWithURL:TLURL(user.avatarURL) placeholderImage:[UIImage imageNamed:DEFAULT_AVATAR_PATH]];
    }
    [self.nicknameLabel setText:user.nikeName];
    [self.wechatIDLabel setText:user.userName ? [NSString stringWithFormat:@"%@：%@",LOCSTR(@"微信号"),user.userName] : @""];
}

- (void)p_addMasonry {
    
    [self.avatarImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(MINE_SPACE_X);
        make.top.mas_equalTo(MINE_SPACE_Y);
        make.bottom.mas_equalTo(-MINE_SPACE_Y);
        make.width.mas_equalTo(self.avatarImageView.mas_height);
    }];
    
    [self.nicknameLabel setContentCompressionResistancePriority:100 forAxis:UILayoutConstraintAxisHorizontal];
    [self.nicknameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.avatarImageView.mas_right).mas_offset(MINE_SPACE_Y);
        make.bottom.mas_equalTo(self.avatarImageView.mas_centerY).mas_offset(-3.5);
    }];
    
    [self.wechatIDLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.nicknameLabel);
        make.top.mas_equalTo(self.avatarImageView.mas_centerY).mas_offset(5);
    }];
    
    [self.QRImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(-0.5);
        make.right.mas_equalTo(self.contentView.mas_right).mas_offset(-10);
        make.height.and.width.mas_equalTo(18);
    }];
    
}

#pragma mark - Getter/Setter
- (UIImageView *)avatarImageView {
    if (!_avatarImageView) {
        _avatarImageView = [UIImageView new];
        [_avatarImageView.layer setMasksToBounds:YES];
        [_avatarImageView.layer setCornerRadius:5.0f];
        [_avatarImageView.layer setBorderWidth:BORDER_WIDTH_1PX];
        [_avatarImageView.layer setBorderColor:[UIColor lightGrayColor].CGColor];
    }
    return _avatarImageView;
}
- (UILabel *)nicknameLabel {
    if (!_nicknameLabel) {
        _nicknameLabel = [UILabel new];
        [_nicknameLabel setText:LOCSTR(@"用户昵称")];
        [_nicknameLabel setFont:[UIFont systemFontOfSize:17.0f]];
    }
    return _nicknameLabel;
}
- (UILabel *)wechatIDLabel {
    if (!_wechatIDLabel) {
        _wechatIDLabel = [UILabel new];
        [_wechatIDLabel setText:LOCSTR(@"微信号")];
        [_wechatIDLabel setFont:[UIFont systemFontOfSize:14]];
    }
    return _wechatIDLabel;
}
- (UIImageView *)QRImageView {
    if (!_QRImageView) {
        _QRImageView = [UIImageView new];
        _QRImageView.image = [UIImage imageNamed:@"mine_cell_myQR"];
    }
    return _QRImageView;
}

@end
